package com.icss.ah.productPrice.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class ProductPriceService {
	private final Logger errlog = Logger.getLogger("E");
	private final Logger infolog = Logger.getLogger("I");
	@Autowired
	private SqlSessionTemplate sqlSessionTemplate;
	/**
	 * 查询商品价格
	 * @param map
	 * @return
	 */
	public List<Map<String, Object>> getProPrice(Map<String, String> map){
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		try {
			list = sqlSessionTemplate.selectList("ProductPriceBean.getProPrice",map);
			if(list == null || list.size() == 0)
				list = sqlSessionTemplate.selectList("ProductPriceBean.getProPrice1",map);
			infolog.info("ProductPriceService|getProPrice success");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("ProductPriceService|getProPrice",e);
		}
		return list;
	}
	/**
	 * 查询产品价格  起批价
	 * @param modelKey
	 * @param map
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public List getProductPrice(String modelKey,Map<String, String> map) {
		List reValue = new ArrayList();
		try {
			reValue = sqlSessionTemplate.selectList(modelKey,map);
		} catch (Exception ex) {
			ex.printStackTrace();
		} 
		return reValue==null?new ArrayList():reValue;
	}
	/**
	 * 商品变价保存提交
	 * @param map
	 * @return
	 */
	@Transactional
	public boolean modifyProductPriceSave(HashMap<String, Object> map){
		boolean result = false;
		try{
			//删除记录
			sqlSessionTemplate.delete("ProductPriceBean.delProductPrice",map);
			infolog.info("ProductPriceService|modifyProductPriceSave|删除现有变价记录");
			//建议零售价修改
			sqlSessionTemplate.update("ProductPriceBean.updateProRetailPrice",map);
			
			sqlSessionTemplate.insert("ProductPriceBean.addProductPrice", map);
			
//			String opeFlag = map.get("opeFlag").toString();
//			if("save".equals(opeFlag)){
//				map.put("status", "0");
//			} else if("submit".equals(opeFlag)){
//				map.put("status", "1");
//			}
//			//获取变价信息
//			String buyNum0 = map.get("buyNum0").toString();
//			String price0 = map.get("price0").toString();
//			String buyNum1 = map.get("buyNum1").toString();
//			String price1 = map.get("price1").toString();
//			String buyNum2 = map.get("buyNum2").toString();
//			String price2 = map.get("price2").toString();
//			
//			if(!StringUtils.isEmpty(buyNum0)){
//				map.put("uuid", UUID.randomUUID().toString());
//				map.put("sortNum", 1);				
//				map.put("purchasePrice", price0);				
//				map.put("purchaseLow", buyNum0);
//				if(!StringUtils.isEmpty(buyNum1)){
//					map.put("purchaseType", 2);
//					map.put("purchaseUp", Integer.valueOf(buyNum1)-1);
//					//添加第一条数据
//					sqlSessionTemplate.insert("ProductPriceBean.addProductPrice", map);
//					
//					map.put("uuid", UUID.randomUUID().toString());
//					map.put("sortNum", 2);				
//					map.put("purchasePrice", price1);				
//					map.put("purchaseLow", buyNum1);
//					if(!StringUtils.isEmpty(buyNum2)){
//						map.put("purchaseType", 2);
//						map.put("purchaseUp", Integer.valueOf(buyNum2)-1);
//						//添加第二条数据
//						sqlSessionTemplate.insert("ProductPriceBean.addProductPrice", map);
//						
//						//添加第三条数据
//						map.put("uuid", UUID.randomUUID().toString());
//						map.put("sortNum", 3);				
//						map.put("purchasePrice", price2);				
//						map.put("purchaseLow", buyNum2);
//						map.put("purchaseType", 1);
//						map.put("purchaseUp", -1);
//						sqlSessionTemplate.insert("ProductPriceBean.addProductPrice", map);
//						
//					}else{
//						map.put("purchaseType", 1);
//						map.put("purchaseUp", -1);
//						//添加第二条数据
//						sqlSessionTemplate.insert("ProductPriceBean.addProductPrice", map);
//					}	
//				}else{
//					map.put("purchaseType", 1);
//					map.put("purchaseUp", -1);
//					//添加第一条数据
//					sqlSessionTemplate.insert("ProductPriceBean.addProductPrice", map);
//				}
//			}	
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("ProductPriceService|modifyProductPriceSave|异常：",e);
		}
		return result;
	}
	
	/**
	 * 插入 产品价格
	 * @param modelKey
	 * @param map
	 */
	public void addProductPrice(String modelKey,Map<String, Object> map) {
		try {
			sqlSessionTemplate.insert(modelKey,map);			
		} catch (Exception ex) {
			ex.printStackTrace();			
		} 
	}
	/**
	 * 删除 产品价格
	 * @param modelKey
	 * @param map
	 */
	@SuppressWarnings("rawtypes")
	public void delProductPrice(String modelKey, HashMap map) {
		try {
			sqlSessionTemplate.delete(modelKey,map);			
		} catch (Exception ex) {
			ex.printStackTrace();			
		} 
	}
	/**
	 * 商品价格  修改  审核列表
	 * @param modelKey
	 * @param map
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public List getProductAuditList(String modelKey,HashMap map) {
		List list = new ArrayList();
		try {
			list = sqlSessionTemplate.selectList(modelKey,map);
			list = list == null ? new ArrayList() : list;
		} catch (Exception ex) {
			ex.printStackTrace();
			list = new ArrayList();
		} 
		return list;
	}
	/**
	 * 商品价格  修改  审核列表长度
	 * @param modelKey
	 * @param map
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public Long getProductAuditCount(String modelKey,HashMap map) {
		Long reValue = 0l;
		try {
			reValue = (Long)sqlSessionTemplate.selectOne(modelKey,map);
		} catch (Exception ex) {
			ex.printStackTrace();
		} 
		return reValue;
	}
	/**
	 * 审核  价格区间
	 */
	@SuppressWarnings("rawtypes")
	public void modifyProductPrice(String modelKey,HashMap map){
		try {
			sqlSessionTemplate.update(modelKey,map);			
		} catch (Exception ex) {
			ex.printStackTrace();			
		} 
	}

	
	
	/**
	 * price表价格  列表
	 * @param modelKey
	 * @param map
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public List getPriceTableList(String modelKey,HashMap map) {
		List list = new ArrayList();
		try {
			list = sqlSessionTemplate.selectList(modelKey,map);
			list = list == null ? new ArrayList() : list;
		} catch (Exception ex) {
			ex.printStackTrace();
			list = new ArrayList();
		} 
		return list;
	}
	/**
	 * 删除price表 产品价格
	 * @param modelKey
	 * @param map
	 */
	@SuppressWarnings("rawtypes")
	public void delPriceTable(String modelKey, HashMap map) {
		try {
			sqlSessionTemplate.delete(modelKey,map);			
		} catch (Exception ex) {
			ex.printStackTrace();			
		} 
	}
	/**
	 * 历史批次
	 * @param modelKey
	 * @param map
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public int getHistoryVersion(String modelKey,HashMap map) {
		int reValue = 0;
		try {
			reValue = (Integer)sqlSessionTemplate.selectOne(modelKey,map);			
			if(reValue == 0){				
				reValue = 10001;				
			}else{
				reValue = reValue + 1;
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		} 
		return reValue;
	}
	/**
	 * price 记录备份到history表
	 * @param modelKey
	 * @param map
	 */
	public void addPriceToHistory(String modelKey,Map<String, Object> map) {
		try {
			sqlSessionTemplate.insert(modelKey,map);			
		} catch (Exception ex) {
			ex.printStackTrace();			
		} 
	}
	
	/**
	 * update 记录备份到price表
	 * @param modelKey
	 * @param map
	 */
	public void addAuditToPrice(String modelKey,Map<String, Object> map) {
		try {
			sqlSessionTemplate.insert(modelKey,map);			
		} catch (Exception ex) {
			ex.printStackTrace();			
		} 
	}
}
